package com.xframe.xdal.core.model.es;

import com.xframe.xdal.core.model.ConditionValue;
import com.xframe.xdal.core.util.StringUtil;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;

public class TermsQueryBuilder extends BaseQueryBuilder {
    /**
     * 构造函数
     *
     * @param esQueryBuildArgs 构造参数
     */
    public TermsQueryBuilder(String esQueryBuildArgs) {
        super(esQueryBuildArgs);
    }

    @Override
    public QueryBuilder builder(ConditionValue conditionValue) {
        QueryBuilder queryBuilder = null;

        if(StringUtil.isEmpty(esQueryBuildArgs)) {
            queryBuilder = QueryBuilders.termsQuery(conditionValue.getFieldName() + ".keyword",
                    conditionValue.getValsByEs());
        }else {
            queryBuilder = QueryBuilders.termsQuery(conditionValue.getFieldName(),
                    conditionValue.getValsByEs());
        }
        return queryBuilder;
    }
}
